// testing of the 'village' card effect from dominion.c
// +1 card and +2 actions

#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#define MAX_CARD treasure_map

typedef struct gameState gameState;

//print the results of the testing
void print_results(int a, int b){
    double results = ((double)a) / b;
    printf("---------------------\n");
    printf("village card effect -- %.f%% of test cases passed (%d/%d)\n", (results*100), a, b);
}

//sets all the values in the game struct to 0
void init_gamestate(struct gameState *g){
    int i;
    for(i = 0; i < sizeof(struct gameState); i++){
    	((char *)g)[i] = 0;
	}
}

int main(int argc, char **argv) {
    int i, j, k;
    int player, handCount, deckCount;
    int result;
    int good_test, bad_test;
	int randomSeed, test_count;
    gameState g1, g2;
   
    if(argc != 3){
            printf("Usage: cardtest3 [seed] [number of tests]\n");
            return(0);
    }
   
    //initialize vars
    randomSeed = atoi(argv[1]);
    test_count = atoi(argv[2]);
    if(test_count < 0){
            printf("Please provide a positive integer for [number of tests]\n");
            return(0);
    }
    good_test = bad_test = 0;
   
    // set up random number
    SelectStream(1);
    PutSeed((long)randomSeed);
    
    for(i = 0; i<test_count; i++){
    	//create random inputs
    	init_gamestate(&g1);
		init_gamestate(&g2);
    	player = (int)(Random()*3)+1; //random player number
		g1.whoseTurn = g2.whoseTurn = player;
    	handCount = (int)(Random()*MAX_HAND); //random hand size
    	g1.handCount[player] = g2.handCount[player] = handCount;
    	//create the random hand
    	for(j = 0; j < handCount; j++){
    		g1.hand[player][j] = g2.hand[player][j] = (int)(Random()*MAX_CARD);
    	}
    	deckCount = (int)(Random()*5)+1; //random deck size with at least 1 card
    	g1.deckCount[player] = g2.deckCount[player] = deckCount;
    	for(j = 0; j < deckCount; j++){
    		g1.deck[player][j] = g2.deck[player][j] = (int)(Random()*MAX_CARD);
    	}
    	g1.numActions = g2.numActions = (int)(Random()*2); //random number of actions
    	
    	//call the function
    	//add village card to hand
    	g1.hand[player][handCount] = g2.hand[player][handCount] = village;
    	g1.handCount[player]++;
    	g2.handCount[player]++;
    	cardEffect(village, 0,0,0, &g1, handCount, NULL);
    	
    	//check the results
    	result = 0;
    	//make sure we have 2 more actions
    	if(g1.numActions != g2.numActions + 2){
    		printf("Error: have %d actions instead of %d!\n", g1.numActions, g2.numActions+2);
    		result = -1;
    	}
    	
    	//make sure we gained a card
    	if(g1.handCount[player] != g2.handCount[player]){
    		printf("Error: hand has %d cards instead of %d!\n", g1.handCount[player], g2.handCount[player]);
    		result = -1;
    	}
    	
    	//make sure the deck got decremented
    	if(g1.deckCount[player] != g2.deckCount[player] - 1){
			printf("Error: deck has %d cards instead of %d!\n", g1.deckCount[player], g2.deckCount[player]);
    		result = -1;
    	}
    	
    	//make sure the card is in the played pile
    	if(g1.playedCards[g2.playedCardCount] != village){
    		printf("Error: village card isn't in discard pile!\n");
    		result = -1;
    	}
    	//determine if test passed or not
    	if(result == -1) bad_test++;
    	else good_test++;
    }

    //output the results
    print_results(good_test, test_count);
   
    return 0;
}
